var BlogCon = iClass("Blog");
var db = iClass.db;
iClass.method("User", {
	//获取博文列表
	getBlogList: function(type, num, page) {
		var blog_list = db.find_list(BlogCon.dbname, {
			user_id: this._id
		});
		//处理参数
		var total_num = blog_list.length;
		num = ~~num;
		num > 0 || (num = total_num); //num > 0
		page = ~~page;
		page >= 0 || (page = 0); //page >= 0
		var total_page = Math.ceil(total_num / num) || 1;

		//整理数据
		if (type) {
			blog_list = blog_list.filter(function(blog_info) {
				return blog_info.type == type;
			});
		};
		blog_list = blog_list.page(num, page);

		blog_list.reverse().map(function(blog_info) {
			return BlogCon.getInstance(blog_info);
		});
		return {
			total_num: total_num,
			total_page: total_page,
			num: num,
			page: page,
			list: blog_list
		};
	},
	//获取指定博文，以及它的操作权
	getBlog: function(blog_id) {
		var blog = BlogCon.get(blog_id);
		if (!blog) {
			throw "找不到指定博文"
		}
		if (blog.user_id !== this._id) {
			throw "指定博文不属于指定用户，无权进行操作";
		}
		return blog;
	},
	addBlog: function(blog_info) {
		if (this.isLocking()) {
			Throw("用户被冻结，无法进行增删改操作");
		}
		blog_info.user_id = this._id;
		var new_blog = new BlogCon(blog_info);
		return new_blog;
	},
	updateBlog: function(blog_id, blog_info) {
		if (this.isLocking()) {
			Throw("用户被冻结，无法进行增删改操作");
		}
		var blog = this.getBlog(blog_id);
		blog.setInfo(blog_info);
		return blog;
	},
	removeBlog: function(blog_id) {
		if (this.isLocking()) {
			Throw("用户被冻结，无法进行增删改操作");
		}
		var blog = this.getBlog(blog_id);
		blog.remove();
	}
});